﻿//facebook
var appID = "1054655387960274";
var redirectURL = "http://tiengnhatthatde.com/ajax/asp/facebook_login.aspx";  //url se tra lai sau khi dang nhap thanh cong, luu y url nay phai trung khop khi cau hinh app tren facebook
var stateValue = "0";  //ko can thiet
var scope = "email,user_birthday"; //day la nhung thong tin mo rong, nhung thong tin basic van dc lay kem theo http://developers.facebook.com/docs/authentication/permissions/
var responseType = "token"; //token or code, default=code
var display = "popup";  //popup,page,touch 
var linkFaceBook = "http://www.facebook.com/dialog/oauth/?client_id=" + appID + "&redirect_uri=" + redirectURL + "&state=" + stateValue + "&scope=" + scope + "&response_type=" + responseType + "&display=" + display;

//google
var OAUTHURL = 'https://accounts.google.com/o/oauth2/auth?';
var VALIDURL = 'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=';
var SCOPE = 'https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email';
var CLIENTID = '166712925137-5jmr2bjl53dk6trh84ehue1b2gtk9lgv.apps.googleusercontent.com';
var REDIRECT = 'http://localhost:60631/ajax/asp/google_login.aspx';
var LOGOUT = 'http://accounts.google.com/Logout';
var linkGoogle = OAUTHURL + 'scope=' + SCOPE + '&client_id=' + CLIENTID + '&redirect_uri=' + REDIRECT + '&response_type=' + responseType;
function openWindow(filename, winname, width, height) {
    var features, top, left;
    left = (window.screen.width - width) / 2;
    top = (window.screen.height - height) / 2;
    features = "width=" + width + ",height=" + height + ",top=" + top + ",left=" + left;
    void (window.open(filename, winname, features));
}

function sleep(milliseconds) {
    var start = new Date().getTime();
    for (var i = 0; i < 1e7; i++) {
        if ((new Date().getTime() - start) > milliseconds) {
            break;
        }
    }
}
function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1);
        if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
    }
    return "";
}
function login_facebook() {
    openWindow(linkFaceBook, "Facebook Login", 415, 305);
}
function login_google() {
    openWindow(linkGoogle, "Google Login", 415, 305);
}
function login() {
    $(document).ready(function () {
        var username = document.getElementById("username").value;
        username = username.trim();
        var password = document.getElementById("password").value;
        password = password.trim();

        if (username == "" || password == "") {
            document.getElementById("login_msg_area").style.display = "block";
            document.getElementById("login_msg").innerHTML = "Thông tin đăng nhập không đầy đủ";
        }
        else {
            var xhttp;
            xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function () {
                if (xhttp.readyState == 4 && xhttp.status == 200) {
                    document.getElementById("check_login_account").innerHTML = xhttp.responseText;
                    var check = document.getElementById("check_login_account").innerHTML;
                    check = check.trim();
                    if (check == "0") {
                        document.getElementById("login_msg_area").style.display = "block";
                        document.getElementById("login_msg").innerHTML = "Tài khoản hoặc mật khẩu không chính xác";
                        document.getElementById("password").value = "";
                    }
                    else {
                        var xhttp1;
                        xhttp1 = new XMLHttpRequest();
                        xhttp1.onreadystatechange = function () {
                            if (xhttp1.readyState == 4 && xhttp1.status == 200) {
                                location.reload(true);
                            };
                        }
                        xhttp1.open("GET", "../ajax/asp/sign_in.aspx?username=" + username, true);
                        xhttp1.send();
                    }
                }
            };
            xhttp.open("GET", "../ajax/asp/User/check_user_account.aspx?username=" + username + "&password=" + password, true);
            xhttp.send();
        }
    });
}

function reg_user() {
    $(document).ready(function () {
        var name = document.getElementById("txt_fullname").value.trim();
        var username = document.getElementById("txt_user_username").value.trim();
        var pass = document.getElementById("txt_user_pass").value.trim();
        var repass = document.getElementById("txt_user_repass").value.trim();
        var email = document.getElementById("txt_user_email").value.trim();
        var phone = document.getElementById("txt_user_phone").value.trim();
        var day = document.getElementById("reg-day").value;
        var month = document.getElementById("reg-month").value;
        var year = document.getElementById("reg-year").value;
        var sex = "";
        if ($('#reg-male').is(":checked"))
            sex = document.getElementById("reg-male").value;
        if ($('#reg-female').is(":checked"))
            sex = document.getElementById("reg-female").value;

        if (name == "" || name.length < 5) {
            document.getElementById("reg_msg_area").style.display = "block";
            document.getElementById("reg_msg").innerHTML = "Họ và tên quá ngắn"
        }
        else {
            if (username == "" || username.length < 6 || username.length > 18) {
                document.getElementById("reg_msg_area").style.display = "block";
                document.getElementById("reg_msg").innerHTML = "Tên đăng nhập phải từ 6 đến 18 ký tự"
            }
            else {
                if (pass == "" || pass.length < 8 || pass.length > 20) {
                    document.getElementById("reg_msg_area").style.display = "block";
                    document.getElementById("reg_msg").innerHTML = "Mật khẩu phải từ 8 đến 20 ký tự"
                }
                else {
                    if (repass == "" || repass != pass) {
                        document.getElementById("reg_msg_area").style.display = "block";
                        document.getElementById("reg_msg").innerHTML = "Nhập lại Email không chính xác";
                    }
                    else {
                        if (email == "") {
                            document.getElementById("reg_msg_area").style.display = "block";
                            document.getElementById("reg_msg").innerHTML = "Email không được để trống";
                        }
                        else {
                            var check = /@/;
                            if (check.test(email) == false) {
                                document.getElementById("reg_msg_area").style.display = "block";
                                document.getElementById("reg_msg").innerHTML = "Định dạng Email không chính xác";
                            }
                            else {
                                var temp = email.split('@');
                                if (temp.length > 2) {
                                    document.getElementById("reg_msg_area").style.display = "block";
                                    document.getElementById("reg_msg").innerHTML = "Định dạng Email không chính xác";
                                }
                                else {
                                    if (sex == "") {
                                        document.getElementById("reg_msg_area").style.display = "block";
                                        document.getElementById("reg_msg").innerHTML = "Bạn chưa chọn giới tính";
                                    }
                                    else {
                                        if (day == 0) {
                                            document.getElementById("reg_msg_area").style.display = "block";
                                            document.getElementById("reg_msg").innerHTML = "Bạn chưa chọn ngày sinh";
                                        }
                                        else {
                                            if (month == 0) {
                                                document.getElementById("reg_msg_area").style.display = "block";
                                                document.getElementById("reg_msg").innerHTML = "Bạn chưa chọn tháng sinh";
                                            }
                                            else {
                                                if (year == 0) {
                                                    document.getElementById("reg_msg_area").style.display = "block";
                                                    document.getElementById("reg_msg").innerHTML = "Bạn chưa chọn năm sinh";
                                                }
                                                else {
                                                    var xhttp;
                                                    xhttp = new XMLHttpRequest();
                                                    xhttp.onreadystatechange = function () {
                                                        if (xhttp.readyState == 4 && xhttp.status == 200) {
                                                            document.getElementById("check_reg_user").innerHTML = xhttp.responseText;
                                                            var checklogin = document.getElementById("check_reg_user").innerHTML;
                                                            checklogin = checklogin.trim();
                                                            if (checklogin == "1") {
                                                                document.getElementById("reg_msg_area").style.display = "block";
                                                                document.getElementById("reg_msg").innerHTML = "Tài khoản đã tồn tại";
                                                            }
                                                            else {
                                                                var xusername;
                                                                xusername = new XMLHttpRequest();
                                                                xusername.onreadystatechange = function () {
                                                                    if (xusername.readyState == 4 && xusername.status == 200) {
                                                                        document.getElementById("check_reg_user").innerHTML = xusername.responseText;
                                                                        var checklogin = document.getElementById("check_reg_user").innerHTML;
                                                                        checklogin = checklogin.trim();
                                                                        if (checklogin == "2") {
                                                                            document.getElementById("reg_msg_area").style.display = "block";
                                                                            document.getElementById("reg_msg").innerHTML = "Tên đăng nhập tồn tại ký tự không hợp lệ";
                                                                        }
                                                                        else {
                                                                            var xpass;
                                                                            xpass = new XMLHttpRequest();
                                                                            xpass.onreadystatechange = function () {
                                                                                if (xpass.readyState == 4 && xpass.status == 200) {
                                                                                    document.getElementById("check_reg_user").innerHTML = xpass.responseText;
                                                                                    var checklogin = document.getElementById("check_reg_user").innerHTML;
                                                                                    checklogin = checklogin.trim();
                                                                                    if (checklogin == "2") {
                                                                                        document.getElementById("reg_msg_area").style.display = "block";
                                                                                        document.getElementById("reg_msg").innerHTML = "Mật khẩu tồn tại ký tự không hợp lệ";
                                                                                    }
                                                                                    else {
                                                                                        var xhttp1;
                                                                                        xhttp1 = new XMLHttpRequest();
                                                                                        xhttp1.onreadystatechange = function () {
                                                                                            if (xhttp1.readyState == 4 && xhttp1.status == 200) {
                                                                                                location.reload(true);
                                                                                            }
                                                                                        };
                                                                                        xhttp1.open("GET", "../ajax/asp/user/sign_in_user.aspx?name=" + name + "&username=" + username.trim() + "&pass=" + pass.trim() + "&repass=" + repass + "&email=" + email + "&phone=" + phone + "&day=" + day + "&month=" + month + "&year=" + year + "&sex=" + sex, true);
                                                                                        xhttp1.send();
                                                                                    }
                                                                                }
                                                                            };
                                                                            xpass.open("GET", "../ajax/asp/user/check_special_string.aspx?string=" + pass.trim(), true);
                                                                            xpass.send();
                                                                        }
                                                                    }
                                                                };
                                                                xusername.open("GET", "../ajax/asp/user/check_special_string.aspx?string=" + username.trim(), true);
                                                                xusername.send();
                                                            }
                                                        }
                                                        //}
                                                    };
                                                    xhttp.open("GET", "../ajax/asp/user/check_reg_user.aspx?username=" + username.trim(), true);
                                                    xhttp.send();
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    });
}
$(document).ready(function () {
    $("#login").on("click", function () {
        login();
    });
    $(".db-row").click(function () {
        if ($(this).hasClass("selected")) {
            $(this).children(".bangN").children(".muiten").removeClass("fa-rotate-90");
            $(this).children(".bangN").removeClass("w3-leftbar w3-border-teal");
            $(this).children(".exams").animate({ height: 'toggle' }, "fast");
            $(this).removeClass("selected");
        }
        else if ($(this).parent().children(".db-row").hasClass("selected")) {
            $(this).parent().children(".selected").children(".bangN").children(".muiten").removeClass("fa-rotate-90");
            $(this).parent().children(".selected").children(".bangN").removeClass("w3-leftbar w3-border-teal");
            $(this).parent().children(".selected").children(".exams").animate({ height: 'toggle' }, "fast");
            $(this).parent().children(".selected").removeClass("selected");
            $(this).addClass("selected");
            $(this).children(".bangN").children(".muiten").addClass("fa-rotate-90");
            $(this).children(".bangN").addClass("w3-leftbar w3-border-teal");
            $(this).children(".exams").animate({ height: 'toggle' }, "fast");
        }
        else {
            $(this).addClass("selected");
            $(this).children(".bangN").children(".muiten").addClass("fa-rotate-90");
            $(this).children(".bangN").addClass("w3-leftbar w3-border-teal");
            $(this).children(".exams").animate({ height: 'toggle' }, "fast");
        }
    });
    $(".login").on("click", function () {
        var user = document.getElementById("username").value;
        var pass = document.getElementById("password").value;
        var xhttp;
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4 && xhttp.status == 200) {
                document.getElementById("nav_menu").innerHTML = xhttp.responseText;
                $("#login").on("click", function () {
                    login();
                });
                $(".login").on("click", function () {
                    var user = document.getElementById("username").value;
                    var pass = document.getElementById("password").value;
                    var xhttp;
                    xhttp = new XMLHttpRequest();
                    xhttp.onreadystatechange = function () {
                        if (xhttp.readyState == 4 && xhttp.status == 200) {
                            document.getElementById("nav_menu").innerHTML = xhttp.responseText;
                            $("#login").on("click", function () {
                                login();
                                //var x = document.getElementById("login-form");
                                //if (x.className.indexOf("w3-show") == -1) {
                                //    x.className += " w3-show";
                                //} else {
                                //    x.className = x.className.replace(" w3-show", "");
                                //}
                            });

                            //Su dung cho facebook


                            $("#btn_login_facebook").on("click", function () {
                                openWindow(linkFaceBook, "Facebook Login", 415, 305);
                            });

                            $("#btn_login_google").on("click", function () {
                                openWindow(linkGoogle, "Facebook Login", 415, 305);
                            });
                            var checklogin = getCookie("user");
                            if (checklogin != "")
                                location.reload(true);
                        }
                    };
                    xhttp.open("GET", "../ajax/asp/checklogin.aspx?user=" + user + "&pass=" + pass, true);
                    xhttp.send();
                });
                //Su dung cho facebook
                $("#btn_login_facebook").on("click", function () {
                    openWindow(linkFaceBook, "Facebook Login", 415, 305);
                });
                //Su dung cho google
                $("#btn_login_google").on("click", function () {
                    openWindow(linkGoogle, "Facebook Login", 415, 305);
                });
                var checklogin = getCookie("user");
                if (checklogin != "")
                    location.reload(true);
            }
        };
        xhttp.open("GET", "../ajax/asp/checklogin.aspx?user=" + user + "&pass=" + pass, true);
        xhttp.send();
    });

    $(".submit-btn").on("click", function () {
        var id_test = document.getElementById("id_test").innerHTML;
        var minute_remain = document.getElementById('test_time').innerHTML - document.getElementsByClassName('minute_remain')[1].innerHTML;

        var user_result = "";
        var get_result = document.getElementsByClassName("selections");
        for (var i = 0; i < get_result.length; i++) {
            user_result += get_result[i].innerHTML + ",";
        }
        if (user_result == "")
            alert("Bạn không thể nộp bài khi chưa trả lời!");
        else
            window.location = "../ajax/asp/submit.aspx?result=" + user_result + "&id=" + id_test + "&minute=" + minute_remain;

    });
    $("#add_test").click(function () {
        var cer = document.getElementById("ddl_certificate").value;
        if (cer == 0) alert("Bạn chưa chọn bằng!");
        else {
            document.getElementById('thembai').style.display = 'block'
        }
    });
    $(".on").click(function () {
        if ($(this).hasClass("off")) {
            $(this).removeClass("off");
            $(this).removeClass("fa-rotate-180");
            $(this).removeClass("w3-text-red");
            $(this).addClass("w3-text-white");
            document.getElementById("active").innerHTML = '1';
        }
        else {
            $(this).addClass("w3-text-red");
            $(this).removeClass("w3-text-white");
            $(this).addClass("fa-rotate-180");
            $(this).addClass("off");
            document.getElementById("active").innerHTML = '0';
        }

    });

    $(".logout_admin").click(function () {
        location.reload(true);
    });
    $("#add_test").click(function () {
        document.getElementById('new_test_name').value = "";
        document.getElementById('new_test_time').value = "";
        document.getElementById('new_test_start').value = "";
        document.getElementById('new_test_name').value = "";
        document.getElementById("active").innerHTML = '0';
        if ($('.on').hasClass("off")) {

        }
        else {
            $('.on').addClass("w3-text-red");
            $('.on').removeClass("w3-text-white");
            $('.on').addClass("fa-rotate-180");
            $('.on').addClass("off");
        }

        //document.getElementById('thuong').setAttribute("checked", "checked");
        //document.getElementById('thuong').setAttribute("checked", "");
    });

    $(".test_del").on("click", function () {
        var id = document.getElementById("get_id").innerHTML;
        var xhttp;
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4 && xhttp.status == 200) {
                document.getElementById("xoabai").style.display = "none";
                document.getElementById("question_area").style.display = "none";
                document.getElementById("themcauhoi").style.display = "none";
                $('#test' + id).remove();
            }
        };
        xhttp.open("GET", "../ajax/asp/certificate/test_del.aspx?id=" + id, true);
        xhttp.send();

    });
    $(".add_test_detail").on("click", function () {
        document.getElementById('themcauhoi').style.display = 'block';
        document.getElementById("themcauhoi").innerHTML = "";

        var xhttp;
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4 && xhttp.status == 200) {
                document.getElementById("themcauhoi").innerHTML = xhttp.responseText;

                //tiny-mce
                tinymce.remove();
                tinymce.init({
                    selector: 'textarea#add_ques_name',
                    height: 200,
                    width: '75%',
                    plugins: [
                      'advlist autolink lists link image charmap print preview anchor',
                      'searchreplace visualblocks code fullscreen furigana ',
                      'insertdatetime media table contextmenu paste code',
                      'textcolor'
                    ],
                    fontsize_formats: "8px 10px 12px 14px 15px 16px 18px 20px 21px 22px",
                    toolbar: 'fontsizeselect insertfile undo redo | styleselect | bold italic | forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image furigana'

                });

                $(".btn_add_test_detail").on("click", function () {
                    var id = document.getElementById("get_id").innerHTML;

                    //tiny mce
                    //set mce content to 
                    $("#add_ques_name").val(tinymce.activeEditor.getContent());
                    var name = $("#add_ques_name").val();

                    var file = "";

                    var pa1 = document.getElementsByClassName("add_ques_detail")[0].value;
                    var pa2 = document.getElementsByClassName("add_ques_detail")[1].value;
                    var pa3 = document.getElementsByClassName("add_ques_detail")[2].value;
                    var pa4 = document.getElementsByClassName("add_ques_detail")[3].value;
                    var type = document.getElementById("test_detail_type").value;
                    var confirm = "";
                    var thutu = "";
                    var temp = document.getElementsByClassName("thutu");
                    if (temp.length == 0)
                        thutu = "0";
                    else
                        thutu = document.getElementsByClassName("thutu")[temp.length - 1].innerHTML;
                    if ($('#pa1').is(':checked'))
                        confirm = document.getElementById("pa1").value;
                    if ($('#pa2').is(':checked'))
                        confirm = document.getElementById("pa2").value;
                    if ($('#pa3').is(':checked'))
                        confirm = document.getElementById("pa3").value;
                    if ($('#pa4').is(':checked'))
                        confirm = document.getElementById("pa4").value;
                    if (name == "")
                        alert("Nội dung không được để trống");
                    else {
                        if (pa1 == "" || pa2 == "" || pa3 == "" || pa4 == "") {
                            alert("Các câu trả lời không được để trống");
                        }
                        else {
                            if (confirm == "")
                                alert("Bạn chưa chọn đáp án cho câu hỏi");
                            else {
                                if (file != "") {
                                    var file_data = document.getElementById('ques_file').files[0];
                                    if (file_data == null) return; // chưa chọn file
                                    var form_data = new FormData();
                                    form_data.append("ques_file", file_data);
                                    $.ajax({
                                        url: '../ajax/asp/certificate/upload.aspx',
                                        data: form_data,
                                        processData: false,
                                        contentType: false,
                                        type: 'POST',
                                        success: function (data) {
                                            //$("#divTreeView").html(data);
                                        }
                                    });
                                }
                                $.post("../ajax/asp/certificate/add_test_detail.aspx", { 'id': id, 'thutu': thutu, 'name': name, 'pa1': pa1, 'pa2': pa2, 'pa3': pa3, 'pa4': pa4, 'file': file, 'confirm': confirm, 'type': type });
                                var xhttp;
                                xhttp = new XMLHttpRequest();
                                xhttp.onreadystatechange = function () {
                                    if (xhttp.readyState == 4 && xhttp.status == 200) {
                                        document.getElementById("themcauhoi").style.display = "none";
                                        document.getElementsByClassName("test_detail")[type - 1].innerHTML += xhttp.responseText;
                                        $(".question").on("click", function () {
                                            var thutu = this.getElementsByClassName("thutu")[0].innerHTML;
                                            var id = this.getElementsByClassName("ques_id")[0].innerHTML;
                                            document.getElementById("themcauhoi").style.display = "block";
                                            var xhttp;
                                            xhttp = new XMLHttpRequest();
                                            xhttp.onreadystatechange = function () {
                                                if (xhttp.readyState == 4 && xhttp.status == 200) {
                                                    document.getElementById("themcauhoi").innerHTML = xhttp.responseText;

                                                    //tiny-mce
                                                    tinymce.remove();
                                                    tinymce.init({
                                                        selector: 'textarea#edit_ques_name',
                                                        height: 200,
                                                        width: '75%',
                                                        plugins: [
                                                          'advlist autolink lists link image charmap print preview anchor',
                                                          'searchreplace visualblocks code fullscreen furigana ',
                                                          'insertdatetime media table contextmenu paste code',
                                                          'textcolor'
                                                        ],
                                                        fontsize_formats: "8px 10px 12px 14px 15px 16px 18px 20px 21px 22px",
                                                        toolbar: 'fontsizeselect insertfile undo redo | styleselect | bold italic | forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image furigana'

                                                    });

                                                    $(".question_edit").on("click", function () {
                                                        $("#edit_ques_name").val(tinymce.activeEditor.getContent());
                                                        var name = $('#edit_ques_name').val();
                                                        var id = document.getElementById('edit_ques_id').innerHTML;
                                                        var pa1 = document.getElementsByClassName('question_detail')[0].value;
                                                        var pa2 = document.getElementsByClassName('question_detail')[1].value;
                                                        var pa3 = document.getElementsByClassName('question_detail')[2].value;
                                                        var pa4 = document.getElementsByClassName('question_detail')[3].value;
                                                        var thutu = document.getElementById("edit_ques_thutu").innerHTML;
                                                        var file = "";
                                                        var pa1_id = document.getElementById("pa1").value;
                                                        var pa2_id = document.getElementById("pa2").value;
                                                        var pa3_id = document.getElementById("pa3").value;
                                                        var pa4_id = document.getElementById("pa4").value;
                                                        var type = document.getElementById("test_detail_type").value;
                                                        var confirm = "";
                                                        if ($('#pa1').is(':checked'))
                                                            confirm = document.getElementById("pa1").value;
                                                        if ($('#pa2').is(':checked'))
                                                            confirm = document.getElementById("pa2").value;
                                                        if ($('#pa3').is(':checked'))
                                                            confirm = document.getElementById("pa3").value;
                                                        if ($('#pa4').is(':checked'))
                                                            confirm = document.getElementById("pa4").value;
                                                        if (name == "") {
                                                            alert("Nội dung không được để trống!");
                                                        }
                                                        else {
                                                            if (pa1 == "")
                                                                alert("Phương án 1 trống!");
                                                            else {
                                                                if (pa2 == "")
                                                                    alert("Phương án 2 trống!");
                                                                else {
                                                                    if (pa3 == "")
                                                                        alert("Phương án 3 trống");
                                                                    else {
                                                                        if (pa4 == "")
                                                                            alert("Phương án 4 trống");
                                                                        else {
                                                                            if (file != "") {
                                                                                var file_data = document.getElementById('ques_file').files[0];
                                                                                if (file_data == null) return; // chưa chọn file
                                                                                var form_data = new FormData();
                                                                                form_data.append("ques_file", file_data);
                                                                                $.ajax({
                                                                                    url: '../ajax/asp/certificate/upload.aspx',
                                                                                    data: form_data,
                                                                                    processData: false,
                                                                                    contentType: false,
                                                                                    type: 'POST',
                                                                                    success: function (data) {
                                                                                        //$("#divTreeView").html(data);
                                                                                    }
                                                                                });
                                                                            }
                                                                            $.post("../ajax/asp/certificate/question_edited.aspx", { 'id': id, 'thutu': thutu, 'name': name, 'pa1': pa1, 'pa1_id': pa1_id, 'pa2': pa2, 'pa2_id': pa2_id, 'pa3': pa3, 'pa3_id': pa3_id, 'pa4': pa4, 'pa4_id': pa4_id, 'file': file, 'confirm': confirm, 'type': type });
                                                                            sleep(1000);
                                                                            var xhttp;
                                                                            xhttp = new XMLHttpRequest();
                                                                            xhttp.onreadystatechange = function () {
                                                                                if (xhttp.readyState == 4 && xhttp.status == 200) {
                                                                                    document.getElementById("themcauhoi").style.display = "none";
                                                                                    document.getElementById(id).innerHTML = xhttp.responseText;
                                                                                    //tiny-mce
                                                                                    tinymce.remove('textarea');
                                                                                    tinymce.init({
                                                                                        selector: 'textarea#edit_ques_name',
                                                                                        height: 200,
                                                                                        width: '75%',
                                                                                        plugins: [
                                                                                          'advlist autolink lists link image charmap print preview anchor',
                                                                                          'searchreplace visualblocks code fullscreen furigana ',
                                                                                          'insertdatetime media table contextmenu paste code',
                                                                                          'textcolor'
                                                                                        ],
                                                                                        fontsize_formats: "8px 10px 12px 14px 15px 16px 18px 20px 21px 22px",
                                                                                        toolbar: 'fontsizeselect insertfile undo redo | styleselect | bold italic | forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image furigana',
                                                                                        content_css: [
                                                                                          '//fast.fonts.net/cssapi/e6dc9b99-64fe-4292-ad98-6974f93cd2a2.css',
                                                                                          '//www.tinymce.com/css/codepen.min.css'
                                                                                        ]
                                                                                    });

                                                                                    $(".del_modal").click(function () {
                                                                                        document.getElementById('xoacauhoi').style.display = 'block';
                                                                                        document.getElementById('themcauhoi').style.display = 'none';
                                                                                        $(".question").removeClass("selected");
                                                                                        $(this).parent().addClass("selected");
                                                                                    });
                                                                                }
                                                                            };
                                                                            xhttp.open("POST", "../ajax/asp/certificate/question_edited_show.aspx?id=" + id + "&thutu=" + thutu, true);
                                                                            xhttp.send();
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    });

                                                }
                                            };
                                            xhttp.open("GET", "../ajax/asp/certificate/question_edit.aspx?id=" + id + "&thutu=" + thutu, true);
                                            xhttp.send();
                                        });
                                        $(".del_modal").click(function () {
                                            document.getElementById('xoacauhoi').style.display = 'block';
                                            document.getElementById('themcauhoi').style.display = 'none';
                                            $(".question").removeClass("selected");
                                            $(this).parent().addClass("selected");
                                        });
                                    }
                                };
                                xhttp.open("GET", "../ajax/asp/certificate/add_test_detail_show.aspx?id=" + id + "&thutu=" + thutu, true);
                                xhttp.send();
                            }
                        }
                    }

                });

            }
        };
        xhttp.open("GET", "../ajax/asp/certificate/add_test_detail_form.aspx", true);
        xhttp.send();
    });

    $('.test_detail_del').on("click", function () {
        var id = document.getElementsByClassName("selected")[0].getElementsByClassName("ques_id")[0].innerHTML;
        var xhttp;
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4 && xhttp.status == 200) {
                document.getElementById('xoacauhoi').style.display = 'none';
                document.getElementById('themcauhoi').style.display = 'none';
                $('#' + id).remove();
            }
        };
        xhttp.open("GET", "../ajax/asp/certificate/test_detail_del.aspx?id=" + id, true);
        xhttp.send();
    });

    $("#reg-month").on("change", function () {
        var day = document.getElementById("reg-day").value;
        var month = document.getElementById("reg-month").value;
        var year = document.getElementById("reg-year").value;
        if (month == 2) {
            if (day > 29) {
                if (year % 4 == 0)
                    document.getElementById("reg-day").value = 29;
                else
                    document.getElementById("reg-day").value = 28;
            }
        }
        else {
            if (month == 4 || month == 6 || month == 9 || month == 11) {
                if (day == 31)
                    document.getElementById("reg-day").value = 30;
            }
        }
    });

    $("#reg-day").on("change", function () {
        var day = document.getElementById("reg-day").value;
        var month = document.getElementById("reg-month").value;
        var year = document.getElementById("reg-year").value;

        if (month == 2 && day > 28) {
            if (year % 4 == 0)
                document.getElementById("reg-day").value = 29;
            else
                document.getElementById("reg-day").value = 28;
        }
        if (month == 4 || month == 6 || month == 9 || month == 11) {
            if (day == 31) {
                document.getElementById("reg-day").value = 30;
            }
        }
    });

    $("#reg-year").on("change", function () {
        var day = document.getElementById("reg-day").value;
        var month = document.getElementById("reg-month").value;
        var year = document.getElementById("reg-year").value;

        if (year % 4 == 0) {
            if (month == 2 && day > 29)
                document.getElementById("reg-day").value = 29;
        }
        else {
            if (month == 2 && day > 28)
                document.getElementById("reg-day").value = 28;
        }
    });


    $("#btn_forget").on("click", function () {
        var username = document.getElementById("txt_forget_username").value;
        var email = document.getElementById("txt_forget_email").value;
        document.getElementById("check_forget").innerHTML = '<div class="w3-row" style="text-align:center"><img src="../../../Content/img/loading.gif"/><div>';
        var xhttp;
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4 && xhttp.status == 200) {
                document.getElementById("check_forget").innerHTML = xhttp.responseText;
                $("#btn_forget").on("click", function () {
                    var username = document.getElementById("txt_forget_username").value;
                    var email = document.getElementById("txt_forget_email").value;
                    document.getElementById("check_forget").innerHTML = '<div class="w3-row" style="text-align:center"><img src="../../../Content/img/loading.gif"/><div>';
                    var xhttp;
                    xhttp = new XMLHttpRequest();
                    xhttp.onreadystatechange = function () {
                        if (xhttp.readyState == 4 && xhttp.status == 200) {
                            document.getElementById("check_forget").innerHTML = xhttp.responseText;
                            $("#btn_forget").on("click", function () {
                                var username = document.getElementById("txt_forget_username").value;
                                var email = document.getElementById("txt_forget_email").value;
                                var xhttp;
                                xhttp = new XMLHttpRequest();
                                xhttp.onreadystatechange = function () {
                                    if (xhttp.readyState == 4 && xhttp.status == 200) {
                                        document.getElementById("check_forget").innerHTML = xhttp.responseText;
                                        $("#btn_forget").on("click", function () {
                                            var username = document.getElementById("txt_forget_username").value;
                                            var email = document.getElementById("txt_forget_email").value;
                                            var xhttp;
                                            xhttp = new XMLHttpRequest();
                                            xhttp.onreadystatechange = function () {
                                                if (xhttp.readyState == 4 && xhttp.status == 200) {
                                                    document.getElementById("check_forget").innerHTML = xhttp.responseText;
                                                }
                                            };
                                            xhttp.open("GET", "../ajax/asp/forget_password.aspx?username=" + username + "&email=" + email, true);
                                            xhttp.send();

                                        });
                                    }
                                };
                                xhttp.open("GET", "../ajax/asp/forget_password.aspx?username=" + username + "&email=" + email, true);
                                xhttp.send();

                            });
                        }
                    };
                    xhttp.open("GET", "../ajax/asp/forget_password.aspx?username=" + username + "&email=" + email, true);
                    xhttp.send();

                });
            }
        };
        xhttp.open("GET", "../ajax/asp/forget_password.aspx?username=" + username + "&email=" + email, true);
        xhttp.send();

    });

    $("#staff-edit-month").on("change", function () {
        var day = document.getElementById("staff-edit-day").value;
        var month = document.getElementById("staff-edit-month").value;
        var year = document.getElementById("staff-edit-year").value;
        if (month == 2) {
            if (day > 29) {
                if (year % 4 == 0)
                    document.getElementById("staff-edit-day").value = 29;
                else
                    document.getElementById("staff-edit-day").value = 28;
            }
        }
        else {
            if (month == 4 || month == 6 || month == 9 || month == 11) {
                if (day == 31)
                    document.getElementById("staff-edit-day").value = 30;
            }
        }
    });

    $("#staff-edit-day").on("change", function () {
        var day = document.getElementById("staff-edit-day").value;
        var month = document.getElementById("staff-edit-month").value;
        var year = document.getElementById("staff-edit-year").value;

        if (month == 2 && day > 28) {
            if (year % 4 == 0)
                document.getElementById("staff-edit-day").value = 29;
            else
                document.getElementById("staff-edit-day").value = 28;
        }
        if (month == 4 || month == 6 || month == 9 || month == 11) {
            if (day == 31) {
                document.getElementById("staff-edit-day").value = 30;
            }
        }
    });

    $("#staff-edit-year").on("change", function () {
        var day = document.getElementById("staff-edit-day").value;
        var month = document.getElementById("staff-edit-month").value;
        var year = document.getElementById("staff-edit-year").value;

        if (year % 4 == 0) {
            if (month == 2 && day > 29)
                document.getElementById("staff-edit-day").value = 29;
        }
        else {
            if (month == 2 && day > 28)
                document.getElementById("staff-edit-day").value = 28;
        }
    });



    $("#btn_edit_staff_pass").on("click", function () {
        var oldpass = document.getElementById("txt_edit_staff_oldpass").value.trim();
        var newpass = document.getElementById("txt_edit_staff_newpass").value.trim();
        var repass = document.getElementById("txt_edit_staff_repass").value.trim();

        if (newpass.length < 10) {
            if ($("#messenge_pass_staff").hasClass("w3-hide"))
                $("#messenge_pass_staff").removeClass("w3-hide");
            document.getElementById("messenge_pass_staff").innerHTML = "Độ dài mật khẩu mới quá ngắn";
        }
        else {
            if (newpass != repass) {
                if ($("#messenge_pass_staff").hasClass("w3-hide"))
                    $("#messenge_pass_staff").removeClass("w3-hide");
                document.getElementById("messenge_pass_staff").innerHTML = "Nhập lại mật khẩu không chính xác";
            }
            else {
                var xhttp;
                xhttp = new XMLHttpRequest();
                xhttp.onreadystatechange = function () {
                    if (xhttp.readyState == 4 && xhttp.status == 200) {
                        document.getElementById("password_edit").innerHTML = xhttp.responseText;
                        $("#btn_edit_staff_pass").on("click", function () {
                            var oldpass = document.getElementById("txt_edit_staff_oldpass").value.trim();
                            var newpass = document.getElementById("txt_edit_staff_newpass").value.trim();
                            var repass = document.getElementById("txt_edit_staff_repass").value.trim();

                            if (newpass.length < 6) {
                                if ($("#messenge_pass_staff").hasClass("w3-hide"))
                                    $("#messenge_pass_staff").removeClass("w3-hide");
                                document.getElementById("messenge_pass_staff").innerHTML = "Độ dài mật khẩu mới quá ngắn";
                            }
                            else {
                                if (newpass != repass) {
                                    if ($("#messenge_pass_staff").hasClass("w3-hide"))
                                        $("#messenge_pass_staff").removeClass("w3-hide");
                                    document.getElementById("messenge_pass_staff").innerHTML = "Nhập lại mật khẩu không chính xác";
                                }
                                else {
                                    var xhttp;
                                    xhttp = new XMLHttpRequest();
                                    xhttp.onreadystatechange = function () {
                                        if (xhttp.readyState == 4 && xhttp.status == 200) {
                                            document.getElementById("password_edit").innerHTML = xhttp.responseText;

                                        }
                                    };
                                    xhttp.open("GET", "../ajax/asp/staff/edit_pass.aspx?oldpass=" + oldpass + "&newpass=" + newpass, true);
                                    xhttp.send();
                                }
                            }
                        });
                    }
                };
                xhttp.open("GET", "../ajax/asp/staff/edit_pass.aspx?oldpass=" + oldpass + "&newpass=" + newpass, true);
                xhttp.send();
            }
        }
    });

    $("#btn_edit_user_pass").on("click", function () {
        var oldpass = document.getElementById("txt_edit_user_oldpass").value.trim();
        var newpass = document.getElementById("txt_edit_user_newpass").value.trim();
        var repass = document.getElementById("txt_edit_user_repass").value.trim();

        if (newpass.length < 6) {
            if ($("#messenge_pass_user").hasClass("w3-hide"))
                $("#messenge_pass_user").removeClass("w3-hide");
            document.getElementById("messenge_pass_user").innerHTML = "Độ dài mật khẩu mới quá ngắn";
        }
        else {
            if (newpass != repass) {
                if ($("#messenge_pass_user").hasClass("w3-hide"))
                    $("#messenge_pass_user").removeClass("w3-hide");
                document.getElementById("messenge_pass_user").innerHTML = "Nhập lại mật khẩu không chính xác";
            }
            else {
                var xhttp;
                xhttp = new XMLHttpRequest();
                xhttp.onreadystatechange = function () {
                    if (xhttp.readyState == 4 && xhttp.status == 200) {
                        document.getElementById("password_edit_user").innerHTML = xhttp.responseText;
                        $("#btn_edit_user_pass").on("click", function () {
                            var oldpass = document.getElementById("txt_edit_user_oldpass").value.trim();
                            var newpass = document.getElementById("txt_edit_user_newpass").value.trim();
                            var repass = document.getElementById("txt_edit_user_repass").value.trim();

                            if (newpass.length < 10) {
                                if ($("#messenge_pass_user").hasClass("w3-hide"))
                                    $("#messenge_pass_user").removeClass("w3-hide");
                                document.getElementById("messenge_pass_user").innerHTML = "Độ dài mật khẩu mới quá ngắn";
                            }
                            else {
                                if (newpass != repass) {
                                    if ($("#messenge_pass_user").hasClass("w3-hide"))
                                        $("#messenge_pass_user").removeClass("w3-hide");
                                    document.getElementById("messenge_pass_user").innerHTML = "Nhập lại mật khẩu không chính xác";
                                }
                                else {
                                    var xhttp;
                                    xhttp = new XMLHttpRequest();
                                    xhttp.onreadystatechange = function () {
                                        if (xhttp.readyState == 4 && xhttp.status == 200) {
                                            document.getElementById("password_edit_user").innerHTML = xhttp.responseText;

                                        }
                                    };
                                    xhttp.open("GET", "../ajax/asp/edit_pass.aspx?oldpass=" + oldpass + "&newpass=" + newpass, true);
                                    xhttp.send();
                                }
                            }
                        });
                    }
                };
                xhttp.open("GET", "../ajax/asp/edit_pass.aspx?oldpass=" + oldpass + "&newpass=" + newpass, true);
                xhttp.send();
            }
        }
    });
    $("#btn_edit_info_user").on("click", function () {
        var fullname = document.getElementById("txt_edit_user_fullname").value;
        var email = document.getElementById("txt_edit_user_email").value;
        var day = document.getElementById("staff-edit-day").value;
        var month = document.getElementById("staff-edit-month").value;
        var year = document.getElementById("staff-edit-year").value;

        var birthday = year + "-" + month + "-" + day;
        var sex = "1";
        if ($('#edit_user_female').is(":checked"))
            sex = "0";

        if (fullname.length == "" || fullname.length < 6) {
            $("#messenge_edit_user").removeClass("w3-hide");
            document.getElementById("messenge_edit_user").innerHTML = "Họ và tên quá ngắn!";
        }
        else {
            if (email.length == "") {
                $("#messenge_edit_user").removeClass("w3-hide");
                document.getElementById("messenge_edit_user").innerHTML = "Email không được để trống!";
            }
            else {
                var check = /@/;
                if (check.test(email) == false) {
                    $("#messenge_edit_user").removeClass("w3-hide");
                    document.getElementById("messenge_edit_user").innerHTML = "Định dạng email không chính xác!";
                }
                else {
                    var temp = email.split('@');
                    if (temp.length > 2) {
                        $("#messenge_edit_user").removeClass("w3-hide");
                        document.getElementById("messenge_edit_user").innerHTML = "Định dạng email không chính xác!";
                    }
                    else {
                        if (temp[1].toLowerCase() != "gmail.com" && temp[1].toLowerCase() != "yahoo.com" && temp[1].toLowerCase() != "yahoo.com.vn" && temp[1].toLowerCase() != "aol.com" && temp[1].toLowerCase() != "outlook.com") {
                            $("#messenge_edit_user").removeClass("w3-hide");
                            document.getElementById("messenge_edit_user").innerHTML = "Email @" + temp[1] + " không tồn tại hoặc không hỗ trợ!";
                        }
                        else {
                            //alert(fullname + " " + email + " " + birthday + " " + sex);
                            var xhttp;
                            xhttp = new XMLHttpRequest();
                            xhttp.onreadystatechange = function () {
                                if (xhttp.readyState == 4 && xhttp.status == 200) {
                                    location.reload(true);
                                }
                            };
                            xhttp.open("GET", "../ajax/asp/edit_info.aspx?name=" + fullname + "&email=" + email + "&birthday=" + birthday + "&sex=" + sex, true);
                            xhttp.send();
                        }
                    }
                }
            }
        }
    });




    $("#btn_edit_avatar_user").on("click", function () {
        var img = document.getElementById("fileUpload").value;
        if (img != "") {
            var file_data = document.getElementById('fileUpload').files[0];
            if (file_data == null) return; // chưa chọn file
            var form_data = new FormData();
            form_data.append("fileUpload", file_data);
            $.ajax({
                url: '../ajax/asp/upload_avatar.aspx',
                data: form_data,
                processData: false,
                contentType: false,
                type: 'POST',
                success: function (data) {
                    //$("#divTreeView").html(data);
                }
            });
        }
        var xhttp;
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4 && xhttp.status == 200) {
                location.reload(true);
            }
        };
        xhttp.open("GET", "../ajax/asp/edit_avatar.aspx?img=" + img, true);
        xhttp.send();
    });

    function openWindow(filename, winname, width, height) {
        var features, top, left;
        left = (window.screen.width - width) / 2;
        top = (window.screen.height - height) / 2;
        features = "width=" + width + ",height=" + height + ",top=" + top + ",left=" + left;
        void (window.open(filename, winname, features));
    }
    $("#btn_login_facebook").on("click", function () {
        openWindow(linkFaceBook, "Facebook Login", 415, 305);
    });

    $("#btn_login_google").on("click", function () {
        openWindow(linkGoogle, "Facebook Login", 415, 305);
    });

    $("#btn_forget_pass_staff").on("click", function () {
        var username = document.getElementById("txt_forget_username_staff").value;
        var email = document.getElementById("txt_forget_email_staff").value;
        document.getElementById("check_forget_staff").innerHTML = '<div class="w3-row" style="text-align:center"><img src="../../../Content/img/loading.gif"/><div>';
        var xhttp;
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4 && xhttp.status == 200) {
                document.getElementById("check_forget_staff").innerHTML = xhttp.responseText;
                $("#btn_forget_pass_staff").on("click", function () {
                    var username = document.getElementById("txt_forget_username_staff").value;
                    var email = document.getElementById("txt_forget_email_staff").value;
                    document.getElementById("check_forget_staff").innerHTML = '<div class="w3-row" style="text-align:center"><img src="../../../Content/img/loading.gif"/><div>';
                    var xhttp;
                    xhttp = new XMLHttpRequest();
                    xhttp.onreadystatechange = function () {
                        if (xhttp.readyState == 4 && xhttp.status == 200) {
                            document.getElementById("check_forget_staff").innerHTML = xhttp.responseText;
                        }
                    };
                    xhttp.open("GET", "../ajax/asp/staff/forget_password.aspx?username=" + username + "&email=" + email, true);
                    xhttp.send();

                });
            }
        };
        xhttp.open("GET", "../ajax/asp/staff/forget_password.aspx?username=" + username + "&email=" + email, true);
        xhttp.send();
    });

    $(".change_level").on("change", function () {
        document.getElementById("level").style.display = "block";
        document.getElementById("confirm_id_user").innerHTML = this.parentElement.parentElement.getElementsByClassName("user_id")[0].innerHTML;
        document.getElementById("confirm_level_user").innerHTML = this.value;
    });

    $("#level_yes").on("click", function () {
        var id = document.getElementById("confirm_id_user").innerHTML;
        var level = document.getElementById("confirm_level_user").innerHTML;
        var xhttp;
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4 && xhttp.status == 200) {
                document.getElementById("level").style.display = 'none';
            }
        };
        xhttp.open("GET", "../ajax/asp/user/change_level.aspx?id=" + id + "&level=" + level, true);
        xhttp.send();
    });

});
function move() {
    var elem = document.getElementById("myBar");
    var width = 1;
    var id = setInterval(frame, 10);
    function frame() {
        if (width >= 70) {
            document.getElementById("myP").className = "w3-text-white w3-xlarge w3-center w3-animate-opacity";
            document.getElementById("myP").innerHTML = "Chào mừng bạn đến với Tiếng Nhật Thật Dễ Admin !";

        }
        if (width >= 100) {
            clearInterval(id);
            document.getElementById("page-loading").className = "w3-hide";
            document.getElementById("login-form").className = "w3-animate-opacity w3-row";
            /*document.getElementById("login-form").className = "w3-hide-small w3-hide-medium w3-animate-opacity w3-row";
            document.getElementById("login-form-medium").className = "w3-hide-small w3-hide-large w3-animate-opacity w3-row";
            document.getElementById("login-form-small").className = "w3-hide-medium w3-hide-large w3-animate-opacity w3-row";*/
            document.getElementById("default-footer").className = "default-footer w3-hide-medium w3-hide-small w3-row w3-padding w3-bottom";
            document.getElementById("footer-loading").className = "w3-hide-large default-footer w3-row w3-padding w3-animate-bottom w3-margin-top";
        }
        else {
            width++;
            elem.style.width = width + '%';
            document.getElementById("demo").innerHTML = width * 1 + '%';
        }
    }
}

$(document).ready(function () {
    $('[data-toggle="tooltip"]').tooltip();
});

$(document).ready(function () {
    $('[data-toggle="popover"]').popover();
});